import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class ICompare implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        //return o1 - o2;
        return o2.compareTo(o1);
    }
}

public class Test {

    public static void main(String[] args) {
        Heap heap = new Heap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        heap.init(array);
        heap.createHeap();
        heap.heapSort();
    }

    /**
     * TOPK解法 取前K个元素放到创建的大根堆中
     * 遍历剩下的N-K个元素，每个元素与堆顶元素比较，如果小于堆顶元素，则删除堆顶元素，再把小于堆顶元素的值放到堆中
     * @param arr
     * @param k
     * @return
     */
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr == null || k == 0) {
            return ret;
        }

        //1.创建大根堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });

        //2.遍历数组的前K个元素 放到大根堆中
        for (int i = 0; i < k; i++) {
            maxHeap.offer(arr[i]);
        }

        //3.遍历剩下的N-K个元素，每个元素与堆顶元素比较，如果小于堆顶元素，则删除堆顶元素
        for (int i = k; i < arr.length; i++) {
            int peekVal = maxHeap.peek();
            if (peekVal > arr[i]) {
                maxHeap.poll();
                maxHeap.offer(arr[i]);
            }
        }

        //4.把元素放到数组中
        for (int i = 0; i < k; i++) {
            ret[i] = maxHeap.poll();
        }
        return ret;
    }

    /**
     * 小根堆
     * @param arr
     * @param k
     * @return
     */
    public int[] smallestK1(int[] arr, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }

    public static void main3(String[] args) {
        ICompare iCompare = new ICompare();
        //传入自己写的比较器，转换为大根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(iCompare);
        priorityQueue.offer(5);
        priorityQueue.offer(7);
        System.out.println(priorityQueue.peek());   //通过自己写的比较器，转换为大根堆

        PriorityQueue<Integer> priorityQueue2 = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });

        //从小到大
        PriorityQueue<Integer> priorityQueue3 = new PriorityQueue<>((x,y) -> x.compareTo(y));
        priorityQueue3.offer(5);
        priorityQueue3.offer(7);
        System.out.println(priorityQueue3.peek());



    }

    public static void main2(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(5);
        priorityQueue.offer(7);
        priorityQueue.offer(9);
        priorityQueue.offer(2);
        System.out.println(priorityQueue.peek()); //默认是小根堆
        System.out.println(priorityQueue.poll());
    }




    public static void main1(String[] args) {
        Heap heap = new Heap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        heap.init(array);
        heap.createHeap();
        heap.offer(78);
        int poll = heap.poll();
        System.out.println(poll);
        int peek = heap.peek();
        System.out.println(peek);
    }

}
